'''
Created on May 2, 2012

@author: lavang
'''
import unittest
from Interface.calendarPage import xpathBtnGoToCalendar, xpathLinkAddEvent,\
    xpathChkEventType, xpathChkDay, xpathChkMonth, xpathChkYear, xpathChkHour,\
    xpathChkMinute, xpathBtnSubmit
    
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
import time

def clickOnGoToCalendar(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathBtnGoToCalendar())
    element.click()
    
# click link Add Event
def clickOnLinkAddEvent(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkAddEvent())
    element.click()
    
# creatEvent
def createEvent(self,day,month,year,hour,min,event,eventType='private'):
    browser=self.browser
    frame=browser.switch_to_frame("POPUP_FRAME")
    
    element=browser.find_element_by_xpath(xpathChkDay())
    Select(element).select_by_value(day)
    
    element=browser.find_element_by_xpath(xpathChkMonth())
    Select(element).select_by_visible_text(month)
    
    element=browser.find_element_by_xpath(xpathChkYear())
    Select(element).select_by_value(year)
    
    element=browser.find_element_by_xpath(xpathChkHour())
    Select(element).select_by_visible_text(hour)
    
    element=browser.find_element_by_xpath(xpathChkMinute())
    Select(element).select_by_visible_text(min)
    
    element=browser.find_element_by_xpath(xpathChkEventType())
    Select(element).select_by_visible_text(eventType)
    
    frame=browser.switch_to_frame("data_ifr")
    element=browser.find_element_by_xpath(".//*[@id='tinymce']")
    element.send_keys(Keys.CONTROL,'a')
    element.send_keys(Keys.DELETE)
    time.sleep(2)
    element.send_keys(event)
    
    browser.switch_to_default_content()
    browser.switch_to_frame("POPUP_FRAME")
    element=browser.find_element_by_xpath(xpathBtnSubmit())
    element.click()
    
# Verify event created
def isEventCreatedSuccessfully(self,eventName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//p[text()='"+eventName+"']"))
        return True
    except:
        return False
    
# show all event
def showAllEvent(self):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'Show all events')]")
    element.click()
    